package com.yty.demo._12_io;

import java.io.File;
import java.io.IOException;

/**
 * @author: yangtaoyao
 * @time: 2020/1/15 15:50
 * @description: 文件操作
 */
public class FileTest {
    public static void main(String[] args) throws IOException {
        File f=new File("com/yty/demo/_12_io/txt/test1.txt");//相对路径
        File f1=new File("com/yty/demo/");

        System.out.println(f.getName());
        System.out.println(f1.getName());
        System.out.println(f.getParent());
        System.out.println(f.getPath());
        System.out.println(f.getAbsolutePath());
        System.out.println(f.getCanonicalPath());
        System.out.println(f.getAbsoluteFile());//返回一个用当前的文件的绝对路径构建的file对象

        f.renameTo(new File("com/yty/demo/_12_io//txt//test2.txt"));
        System.out.println(f.getName());

        File f2=new File("com/yty/demo/_12_io/test2.txt");
        System.out.println(f2.exists());
        System.out.println(f2.canWrite());
        System.out.println(f2.canRead());
        System.out.println(f2.isFile());
        System.out.println(f2.isDirectory());

        System.out.println(f2.lastModified());
        System.out.println(f2.length());//文件长度，字节数

        File f3=new File("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\test1\\test11");
//        f3.mkdir();//创建单层
        System.out.println(f3.mkdirs());//创建多层
//        f3.delete();

        File f4=new File("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\test1");
        String[] fl=f4.list();
        System.out.println("------list-------");
        for (String s : fl) {
            System.out.println(s);
        }

        File[] fl2=f4.listFiles();
        System.out.println("------listFiles-------");
        for (File ff : fl2) {
            System.out.println(ff);
//            ff.delete();
        }

        System.out.println("----------递归遍历------");
        test(f4);
    }

    /**
     * 递归遍历文件
     */
    public static void test(File file){
        if(file.isFile()){
            System.out.println(file.getAbsolutePath()+"是文件");
        }else{
            System.out.println(file.getAbsolutePath()+"是文件夹");
            File[] fs=file.listFiles();
            if(fs!=null&&fs.length>0){
                for (File f : fs) {
                    test(f);
                }
            }
        }
    }
}
